package com.lti.appnet.module.controller;

import com.google.common.collect.Lists;
import com.lti.appnet.common.util.ResponseBundle;
import com.lti.appnet.module.domain.Category;
import com.lti.appnet.module.domain.Module;
import com.lti.appnet.module.event.CategoryCreateEvent;
import com.lti.appnet.module.event.ModuleCreateEvent;
import com.lti.appnet.module.event.ModuleReadEvent;
import com.lti.appnet.module.response.CategoryReq;
import com.lti.appnet.module.response.CategoryRes;
import com.lti.appnet.module.response.ModuleReq;
import com.lti.appnet.module.response.ModuleRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@RequestMapping("/module")
@Controller
@Scope("prototype")
public class ModuleController {

    @Autowired
    ModuleCreateEvent moduleCreateEvent;
    @Autowired
    ModuleReadEvent moduleReadEvent;

    @Autowired
    CategoryCreateEvent categoryCreateEvent;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResponseBundle<List<ModuleRes>> getAuthorModule() {
        List<ModuleRes> modules = moduleReadEvent.getAuthorAllModules("1");
        return new ResponseBundle<List<ModuleRes>>(modules);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseBundle<ModuleRes> saveModule(@RequestBody ModuleReq moduleReq) {
        Module _module = moduleReq.getModule();
        Module module = moduleCreateEvent.saveModule(_module);
        return new ResponseBundle<ModuleRes>(new ModuleRes(module));
    }

    @RequestMapping(value = "/category", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBundle<CategoryRes> saveCategory(@RequestBody CategoryReq categoryReq) {
        Category _category = categoryReq.getCategory();
        Category category = categoryCreateEvent.saveCategory(_category);
        return new ResponseBundle<CategoryRes>(new CategoryRes(category));
    }
}
